///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
#include <GarbageCollector/GarbageCollector.h>
//-----------------------------------------------------------------------------

GarbageCollector::GarbageCollector()
{
	m_autoCollect = false;
	m_autoCollectLimit = 1024;
	m_garbage.reserve(m_autoCollectLimit);
}

//-----------------------------------------------------------------------------

GarbageCollector::~GarbageCollector()
{
	takeOutTrash();
}

//-----------------------------------------------------------------------------

void	GarbageCollector::takeOutTrash()
{
	if( m_garbage.size() )
	{
		std::vector<void*>::iterator it = m_garbage.begin();
		std::vector<void*>::iterator end = m_garbage.end();
		while( it != end )
		{
			free(*it);
			it++;
		}
		m_garbage.clear();
	}
}

//-----------------------------------------------------------------------------

void	GarbageCollector::SetAutomaticCollectionLimit(u32 limit)
{
	takeOutTrash();
	m_autoCollectLimit = limit;
	m_garbage.reserve(m_autoCollectLimit);
}

//-----------------------------------------------------------------------------

void	GarbageCollector::collectGarbage(void* ptr, bool releaseNow)
{
	if( releaseNow )
	{
		free(ptr);
	}
	else
	{
		m_garbage.push_back(ptr);
		if( m_autoCollect && m_autoCollectLimit == m_garbage.size() )
		{
			takeOutTrash();
		}
	}
}

//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
